package cn.itcast.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.service.Sys_menuService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("menu")
@Slf4j
public class Sys_menuController {
    @Reference(version = "1.0.0")
    private Sys_menuService sys_menuService;

    @GetMapping("/page")
    public Result queryMenuList(QueryPageBean queryPageBean){
        try {
            PageResult pageResult= this.sys_menuService.queryMenuList(queryPageBean.getCurrentPage()
                    ,queryPageBean.getPageSize()
                    ,queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_MENU_FAIL);
    }

    @PostMapping
    public Result saveMenu(@RequestBody Map<String, String> map,String permissionIds){
        Boolean aBoolean = this.sys_menuService.saveMenus(map,permissionIds);
        if (aBoolean){
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }
        return new Result(false,MessageConstant.ADD_MENU_FAIL);
    }

    @GetMapping("{id}")
    public Result queryEditMenus(@PathVariable("id") Integer id){
        try {
            Menu menu = this.sys_menuService.queryEditMenus(id);
            if (ObjectUtil.isNotEmpty(menu)){
                return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,menu);
            }
        } catch (Exception e) {
            log.error("查询失败，id="+id,e);
        }
        return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
    }
    @GetMapping("{id}/permissions")
    public Result queryMenusOfPermissions(@PathVariable("id") Integer id){
        if (ObjectUtil.isNotEmpty(id)){
            List<Integer> permissionsIds= this.sys_menuService.queryMenusOfPermissions(id);
            return new  Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,permissionsIds);
        }
        return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);

    }

    @PutMapping
    public Result editMenu(@RequestBody Menu menu,String permissionIds){
        Boolean aBoolean = this.sys_menuService.editMenus(menu,permissionIds);
        if (aBoolean){
            return new Result(true,MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
        }
        return new Result(false,MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
    }


    @DeleteMapping("/{id}")
    private Result delMenu(@PathVariable("id") Integer id){
        try {
            Boolean result = this.sys_menuService.delMenus(id);
            if (result){
                return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
            }
        } catch (Exception e) {
            log.error("删除失败，id="+id,e);
        }
        return new Result(false,MessageConstant.DELETE_MENU_FAIL);
    }
}
